package misp_backend.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import misp_backend.domain.Dishes;
import misp_backend.domain.Package;
import misp_backend.domain.PackageDTO;
import misp_backend.domain.Packagedetails;
import misp_backend.mapper.PackagedetailsMapper;
import misp_backend.service.DishesService;
import misp_backend.service.PackageService;
import misp_backend.mapper.PackageMapper;
import misp_backend.service.PackagedetailsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author yang
* @description 针对表【package】的数据库操作Service实现
* @createDate 2025-03-19 23:53:29
*/
@Service
@Transactional
public class PackageServiceImpl extends ServiceImpl<PackageMapper, Package>
    implements PackageService{
   @Autowired
    private PackageMapper packageMapper;
   @Autowired
   private PackagedetailsService packagedetailsService;
   @Autowired
   private DishesService dishesService;
   List<Long>dishid=new ArrayList<>();
   public PackageDTO listpackdetails(Long packageid){
       PackageDTO packageDTO1=new PackageDTO();
       packageDTO1.setPackageId(packageid);
       Package pack=packageMapper.selectById(packageid);
       packageDTO1.setPackageName(pack.getPackageName());
       packageDTO1.setPrice(pack.getPrice());
       packageDTO1.setDescription(pack.getDescription());
       packageDTO1.setStatus(pack.getStatus());
       packageDTO1.setCover(pack.getCover());
       packageDTO1.setDishes(packageMapper.selectPackageDishes(packageid));
       return packageDTO1;
   }
   public List<PackageDTO> listall(){
       List <PackageDTO> listpack=new ArrayList<>();
     for(Package p  :this.list()){
      listpack.add( listpackdetails(  p.getPackageId()));
     }
     return listpack;
    }
   public boolean addnewpackage(PackageDTO pdto){
       BigDecimal totalprice=new BigDecimal(0);
       boolean result=false;
       List<PackageDTO.PackageDishDTO> list2=pdto.getDishes();
       for(int i=0;i<list2.size();i++){
           QueryWrapper<Dishes>q=new QueryWrapper<>();
           q.eq("dish_name",list2.get(i).getDishName());
           Dishes dishes=dishesService.getOne(q);
           dishid.add(dishes.getDishId());
           totalprice=totalprice.add(BigDecimal.valueOf(list2.get(i).getQuantity()).multiply(dishes.getPrice()));
       }
Package packge=new Package();
packge.setPrice(totalprice.multiply(new BigDecimal(0.85)));
packge.setPackageName(pdto.getPackageName());
packge.setDescription(pdto.getDescription());
packageMapper.insert(packge);
       QueryWrapper<Package> queryWrapper = new QueryWrapper<>();
       queryWrapper.eq("package_name", pdto.getPackageName());
       Package packer2  = packageMapper.selectOne(queryWrapper);

for(int i=0;i<list2.size();i++){
    Dishes dishes=dishesService.getById(list2.get(i).getDishId());
Packagedetails packagedetails=new Packagedetails();
packagedetails.setQuantity(list2.get(i).getQuantity());
packagedetails.setDishId(dishid.get(i));
packagedetails.setPackageId(packer2.getPackageId());
 result=packagedetailsService.save(packagedetails);
}
return  result;
   }
}




